/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Ucop/Crypto/BigNumber.h"
#include <memory.h>
#include <algorithm>
#include <openssl/bn.h>
#include "Ucop/Macros.h"


namespace Ucop {
namespace Crypto {

		BigNumber::BigNumber(void)
			: _Mybn(BN_new())
			, _Myarray()
		{

		}

		BigNumber::BigNumber(const BigNumber& _X)
			: _Mybn(BN_dup(_X._Mybn))
			, _Myarray()
		{

		}

		BigNumber::BigNumber(std::uint32_t _X)
			: _Mybn(BN_new())
			, _Myarray()
		{
			BN_set_word(_Mybn, _X);
		}

		BigNumber::~BigNumber(void)
		{
			BN_free(_Mybn);
		}

		void BigNumber::SetUInt32(std::uint32_t _X)
		{
			BN_set_word(_Mybn, _X);
		}

		void BigNumber::SetUInt64(std::uint64_t _X)
		{
			BN_add_word(_Mybn, (std::uint32_t)(_X >> 32));
			BN_lshift(_Mybn, _Mybn, 32);
			BN_add_word(_Mybn, (std::uint32_t)(_X & 0xFFFFFFFF));
		}

		void BigNumber::SetBinary(const std::uint8_t* _Bytes, int _Len)
		{
			std::uint8_t _T[1000];
			for (int _I = 0; _I < _Len; ++_I) {
				_T[_I] = _Bytes[_Len - 1 - _I];
			}
			BN_bin2bn(_T, _Len, _Mybn);
		}

		void BigNumber::SetHexStr(const char* _Str)
		{
			BN_hex2bn(&_Mybn, _Str);
		}

		void BigNumber::SetRand(int _Numbits)
		{
			BN_rand(_Mybn, _Numbits, 0, 1);
		}

		void BigNumber::Free(void* _X)
		{
			OPENSSL_free(_X);
		}

		BigNumber BigNumber::operator=(const BigNumber& _X)
		{
			BN_copy(_Mybn, _X._Mybn);
			return *this;
		}

		BigNumber BigNumber::operator+=(const BigNumber& _X)
		{
			BN_add(_Mybn, _Mybn, _X._Mybn);
			return *this;
		}

		BigNumber BigNumber::operator-=(const BigNumber& _X)
		{
			BN_sub(_Mybn, _Mybn, _X._Mybn);
			return *this;
		}

		BigNumber BigNumber::operator*=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mul(_Mybn, _Mybn, _X._Mybn, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::operator/=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_div(_Mybn, NULL, _Mybn, _X._Mybn, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::operator%=(const BigNumber& _X)
		{
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mod(_Mybn, _Mybn, _X._Mybn, _Bnctx);
			BN_CTX_free(_Bnctx);

			return *this;
		}

		BigNumber BigNumber::Exp(const BigNumber& _Bn)
		{
			BigNumber _Ret;
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_exp(_Ret._Mybn, _Mybn, _Bn._Mybn, _Bnctx);
			BN_CTX_free(_Bnctx);

			return _Ret;
		}

		BigNumber BigNumber::ModExp(const BigNumber& _Bn1, const BigNumber& _Bn2)
		{
			BigNumber _Ret;
			BN_CTX* _Bnctx;

			_Bnctx = BN_CTX_new();
			BN_mod_exp(_Ret._Mybn, _Mybn, _Bn1._Mybn, _Bn2._Mybn, _Bnctx);
			BN_CTX_free(_Bnctx);

			return _Ret;
		}

		int BigNumber::GetNumBytes(void)
		{
			return BN_num_bytes(_Mybn);
		}

		uint32_t BigNumber::AsUInt32(void)
		{
			return (std::uint32_t)BN_get_word(_Mybn);
		}

		bool BigNumber::IsZero(void) const
		{
			return BN_is_zero(_Mybn) != 0;
		}

		uint8_t* BigNumber::AsByteArray(int _MinSize)
		{
			int _Length = UCOP_MAX(_MinSize, GetNumBytes());
			_Myarray.reset(new std::uint8_t[_Length]);
			UCOP_ASSERT(_Myarray);
			if (!_Myarray) {
				return NULL;
			}
			if (_Length > GetNumBytes()) {
				memset((void*)_Myarray.get(), 0, _Length);
			}
			BN_bn2bin(_Mybn, (unsigned char*)_Myarray.get());
			std::reverse(_Myarray.get(), _Myarray.get() + _Length);
			return _Myarray.get();
		}

		const char* BigNumber::AsHexStr(void)
		{
			return BN_bn2hex(_Mybn);
		}

		const char* BigNumber::AsDecStr(void)
		{
			return BN_bn2dec(_Mybn);
		}

}}